/*
	flad : Copyright 2010 Dale J Williams

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
package lads.flad.screens {
	
	import lads.flad.GameCore;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.Event;
	
	/**
	 * core: Switches the various screens in the core.
	 * @author Dale J Williams
	 * @version 1.2
	 */
	public class ScreenManager {
		
		private var _core		:GameCore;
		private var _parent		:DisplayObjectContainer;
		private var _stage		:Stage;
		private var _screens	:Object;
		private var _screen		:GameScreen;
		private var _prevScreen	:GameScreen;
		private var _switching	:Boolean;
		
		public function ScreenManager($parent:DisplayObjectContainer) {
			_parent = $parent;
			_stage = $parent.stage;
			_screens = {};
		}
		
		public function addScreen($screen:GameScreen, $switchTo:Boolean = false):void {
			if (_screens[$screen.name]) return;
			_screens[$screen.name] = $screen;
			if ($switchTo) switchTo($screen);
		}
		
		public function getScreen($name:String):GameScreen {
			return _screens[$name] as GameScreen;
		}
		
		public function switchTo($screenOrScreenName:Object):void {
			if ($screenOrScreenName is String)
				if (_screens.hasOwnProperty(String($screenOrScreenName)))
					switchToScreen(_screens[$screenOrScreenName]);
				else throw new Error("core contains no screen by that name.");
			else if ($screenOrScreenName is GameScreen)
				switchToScreen(GameScreen($screenOrScreenName));
			else throw new Error("GameScreen is not valid.");
		}
		
		private function switchToScreen($screen:GameScreen):void {
			if ($screen == _screen) return;
			_prevScreen = _screen;
			_screen = $screen;
			_screen.visible = _screen.mouseEnabled = _screen.mouseChildren = true;
			if (_prevScreen) {
				_prevScreen.addEventListener(GameScreen.REMOVED, completeSwitch);
				_prevScreen.animOut();
			} else addNext();
		}
		
		private function completeSwitch(e:Event):void {
			_prevScreen.removeEventListener(GameScreen.REMOVED, completeSwitch);
			_prevScreen.visible = _prevScreen.mouseEnabled = _prevScreen.mouseChildren = false;
			remove(_prevScreen);
			_prevScreen = null;
			addNext();
		}
		
		private function addNext():void {			
			add(_screen);
			_screen.animIn();
			_screen.focusRect = false;
			_screen.parent.setChildIndex(_screen, _screen.parent.numChildren - 1);
			_stage.focus = _screen;
		}
				
		public function add($screen:GameScreen):void {
			$screen.manager = this;
			parent.addChild($screen);
		}
		
		public function remove($screen:GameScreen):void {
			$screen.parent.removeChild($screen);
			$screen.manager = null;
		}
		
		public function get parent():DisplayObjectContainer { return _parent; }
		
		public function get screen():GameScreen { return _screen; }
		
		public function get core():GameCore { return _core; }		
		public function set core(value:GameCore):void {
			_core = value;
		}
		
	}

}